home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
-
- LPDIRECTSOUND DS = 0;
- LPDIRECTSOUNDBUFFER soundcard = 0;
- int no_sound = FALSE;
-
- void init_directsound()
- {
- // Get DirectSound object
-
- if (no_sound || FAILED(DirectSoundCreate(0, &DS, 0)))
- {
- no_sound = TRUE;
- return;
- }
- }
-
- void init_directsound_primarybuffer()
- {
- if (no_sound)
- return;
-
- // Set cooperative level
-
- if(FAILED(DS->SetCooperativeLevel(mainwindowhandle, DSSCL_NORMAL)))
- error("Unable to set cooperative level for sound card");
-
- // Create primary buffer
-
- DSBUFFERDESC dsbd;
-
- ZeroMemory(&dsbd, sizeof(dsbd));
- dsbd.dwSize = sizeof(dsbd);
- dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
-
- if (FAILED(DS->CreateSoundBuffer(&dsbd, &soundcard, 0)))
- error("Unable to create primary sound buffer");
-
- // Start playing silence
-
- soundcard->Play(0, 0, DSBPLAY_LOOPING);
- }
-
- void deinit_directsound()
- {
- safe_release(&soundcard);
- safe_release(&DS);
- }
-
- void play_sound(cWAV *snd, int volume)
- {
- if (no_sound)
- return;
-
- snd->dsb->SetCurrentPosition(0);
- snd->dsb->SetVolume(volume);
- snd->dsb->Play(0,0,0);
- }
-
- void ambient_sounds()
- {
- static cTimer wait;
-
- if (!wait && !no_sound)
- {
- switch(rnd(2))
- {
- case 0:
- play_sound(cWAV::get("ALARM"));
- break;
-
- case 1:
- play_sound(cWAV::get("CREAK"));
- break;
- }
-
- wait = rnd(40 * sec);
- }
- }
-